package com.safewind.wx.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class JsonUtil {
    public static JSONObject toJSONObject(String str) {
        return JSON.parseObject(str);
    }

    public static List<Object> toList(String str) {
        JSONArray array = JSON.parseArray(str);
        List<Object> list = new ArrayList<Object>();
        Iterator<Object> it = array.iterator();
        while (it.hasNext()) {
            Object o = it.next();
            list.add(o);
        }
        return list;
    }

    public static Map<String, Object> toMap(String str) {
        JSONObject obj = JSON.parseObject(str);
        Map<String, Object> map = new HashMap<String, Object>();
        for (String key : obj.keySet()) {
            Object o = obj.get(key);
            map.put(key, o);
        }
        return map;
    }

    public static List<Map<String, Object>> toListMap(String str) {
        JSONArray array = JSON.parseArray(str);
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        Iterator<Object> it = array.iterator();
        while (it.hasNext()) {
            Object o = it.next();
            Map<String, Object> map = toMap(JSON.toJSONString(o));
            list.add(map);
        }
        return list;
    }
}
